package ch.fhnw.qualiscan.application.modals;

import ch.fhnw.qualiscan.Config;
import ch.fhnw.qualiscan.application.models.AdminModel;
import ch.fhnw.qualiscan.application.models.QuestionnaireModel;
import ch.fhnw.qualiscan.application.views.AdminPanelView;
import ch.fhnw.qualiscan.core.Modal;
import ch.fhnw.qualiscan.core.View;
import ch.fhnw.qualiscan.core.db.pojos.Questionnaire;
import ch.fhnw.qualiscan.core.db.pojos.User;

import com.vaadin.ui.Button;
import com.vaadin.ui.Form;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.TextField;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.TextArea;

import eu.livotov.tpt.i18n.TM;
import java.util.Date;

public class AddQuestionnaire extends Modal {
	
	private Form questionnaireForm;
	private QuestionnaireModel qModel;
	private View caller;
	private User user;
	
	public AddQuestionnaire(View caller, User user) {
		this.user = user;
		setCaption(TM.get("admin.questionnaire.add"));
		setWidth("450px");
		this.caller = caller;
		qModel = new QuestionnaireModel();
		buildLayout();
	}
	
	public void buildLayout() {
		questionnaireForm = new Form();

		HorizontalLayout buttonBar = new HorizontalLayout();
		questionnaireForm.getFooter().addComponent(buttonBar);
		
		TextField titleField = new TextField(); 
		titleField.setColumns(20);
		titleField.setWidth("400px");
		titleField.isRequired();
		titleField.setCaption(TM.get("admin.questionnaire.add.title"));
		questionnaireForm.addField("titleField", titleField);
		
                TextArea descField = new TextArea();
                descField.setColumns(20);
                descField.setRows(3);
                descField.setCaption(TM.get("admin.questionnaire.add.desc"));
                questionnaireForm.addField("descField", descField);
                
		Button saveButton = new Button(TM.get("action.save"), new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
                            questionnaireForm.commit();
                            String title = (String) questionnaireForm.getField("titleField").getValue();
                            String desc = (String) questionnaireForm.getField("descField").getValue();
                            
                            Questionnaire questionnaire = new Questionnaire();
                            questionnaire.setTitle(title);
                            questionnaire.setDescription(desc);
                            questionnaire.setActive(0);
                            Date date = new Date();
                            questionnaire.setCreated(date);
                            if (user != null) {
                            	questionnaire.setUser(user);
                            }
                            
                            if(!title.equals("")) {
                                qModel.addQuestionnaire(questionnaire);
                                caller.reBuild();
                                closeModal();
                                getWindow().showNotification(TM.get("admin.questionnaire.added"));
                            } else {
                                getWindow().showNotification(TM.get("admin.questionnaire.notadded"));
                            }
                            
			}
		});
		
		saveButton.setData(this);
		saveButton.setIcon(Config.ICON_SAVE);
		buttonBar.addComponent(saveButton);
		addComponent(questionnaireForm);
	}
	
}
